/* =========================================================================
=  this link list program can be used to form table for compression        =
=   which is Header File can Be used by other program to use               =
=   for Building Table                                                     =
=	   -occurence of char in file and number of time it does               =
=                                                                          =
=   *it will sort the file according to the number of time it occures      =
=   *display the table(sorted or unsorted)                                 =
=                                                                          =
=  @ ISSC By                                                               =
=		 AUTHOR  VISHWAJEET DUSANE(dusanevishwajeet@yahoo.co.in)           =
=                                                                          =
=  What this                                                               =
=	 -build table i.e. occurence of each char in the file                  =
=	 -sort them according to the number of char in file in descending order=
============================================================================
*/
#include<stdio.h>

#include<iostream.h>
#include<alloc.h>
//#include"Declare.h"
struct node
{
float Value;
struct node *next;
};

class Table
{
private:
	 FILE *fp;
public:
void Build(char [],struct node **);
int search(struct node **,int );
void create(struct node **,float);
void SortTable(struct node **);
int getPosition(struct node *,int);
int getCharacter(struct node **,int);
int getTotalChar(struct node *);
float getValue(struct node *,int );
void replaceNumber(char fname[30]);
};

float Table::getValue(struct node *temp,int position)
{
   int cnt=0;
	while(temp!=NULL)
	{
	 if(position==cnt)
	 return temp->value;
	 cnt++;
	 temp=temp->next;
	}
 return -1;

}
int Table:: getTotalChar(struct node *temp)
{
   int i=0;
	while(temp!=NULL)
	{
	 i++;
	 temp=temp->next;
	}
  return i;
}

 void Table:: create(struct node **head,float value)
 {
   struct node *temp=*head;

	if(*head==NULL)
	  {
	   *head=(struct node *)malloc(sizeof(struct node));
	  (*head)->next=NULL;
	(*head)->value=value;

	   }
	  else
	  {
	  temp=*head;
	  while(temp->next!=NULL)
	  temp=temp->next;
	  temp->next=(struct node *)malloc(sizeof(struct node));

	  temp=temp->next;
	  temp->next=NULL;
	 temp->value=value;

	   }
 }
 int Table::getCharacter(struct node **head,int pos)
 {
   struct node *temp=*head;

	if(*head==NULL)
	  {
	  return NULL;
	  }
	  else
	  {
		 temp=*head;
			 while(temp!=NULL)
			 {
				 pos--;
				 if(pos==0)
				  return temp->ch;
				temp=temp->next;
			 }
	  }
  return NULL;

 }


 void Table::replaceNumber(char fname[30])
{
   FILE *fp,*fp1;
   int ch;
	fp=fopen(fname,"r");
	fp1=fopen("Huffnum.txt","w");
	if(fp==NULL||fp1==NULL)
	{
	printf("File Not Found");

	exit(0);
	}
	while((ch=fgetc(fp))!=EOF)
	 fprintf(fp1,"%d",ch);
   fcloseall();
}
int Table:: search(struct node **head,int ch)
{
   struct node *temp=*head;

	if(*head==NULL)
	  {
	  return 1;
	  }
	  else
	  {
	 temp=*head;
	  while(temp!=NULL)
	  {
		if(temp->ch==ch)
		{
		 temp->counter++;
		 return 0;
		}
	  temp=temp->next;
	  }
	   }
  return 1;
}

int Table::getPosition(struct node *temp,int ch)
{
	int pos=0;
	if(temp==NULL)
	  {
	  return 0;
	  }
	  else
	  {
	  while(temp!=NULL)
	   {
	   pos++;
		if(temp->ch==ch)
		{
		 return pos;
		}
	  temp=temp->next;
	  }
	   }
  return 0;
}

void Table:: SortTable(struct node **head)
{
	struct node *temp,*temp1;
	int tempCounter,tempCh;
	int i,j;

  if(*head==NULL)
	return;
  else
  {
	temp=*head;
   while(temp!=NULL)
  {
	 temp1=*head;
	while(temp1!=NULL)
	{
	   if( ( temp1 -> counter ) < ( temp -> counter ) )
		{
		 tempCounter = temp1 -> counter;
		 temp1 -> counter = temp -> counter;
		 temp -> counter = tempCounter;
		 tempCh = temp1 -> ch;
		 temp1 -> ch = temp -> ch;
		 temp -> ch = tempCh;
		}
		 temp1 = temp1 -> next;
	}
	temp=temp->next;
  }
}
}


void Table::Build(char fname[],struct node **head)
   {
	 struct node *Temphead=NULL;
	 char ch;
	 int result;
     fp=fopen(fname,"rb");
	 if(fp==NULL)
	 {
	 printf("Error While Openonig  %s",fname);

	 exit(0);
	 }

	while((ch=fgetc(fp))!=EOF)
	{
	 result=search(&Temphead,ch);/* whether the char is in the table or not
								   if it is in then no need to add that in
								   the table search will automatically
								   increment its count
							   */

	 if(result!=0)
	 create(&Temphead,ch);
	}
   //	BT.display(head);

	 SortTable(&Temphead);   /*This function can sort the nodes according to
						   the occurrences of a charcter
						 */

	//  BT.display(head);
						 /*this function will display the sorted nodes
							can be used for display (those which r not sorted)
							sort it in descending order
						*/

  fclose(fp);
  *(head)=Temphead;

}

